#include <algorithm>
#include <cstdio>
#include <cstring>
#define lowbit(x) (x & -x)

typedef long long ll;
const int N = 2e4 + 5e3 + 5;
int n, k, a[N], L = 1, R;
ll val[N], f[26][N], ans;

inline ll query(int x) {
  ll res = 0;
  while (x) res += val[x], x -= lowbit(x);
  return res;
}

inline void modify(int x, int v) {
  while (x <= n) val[x] += v, x += lowbit(x);
}

inline ll calc(int l, int r) {
  while (R < r) {
    ans += query(a[++R]);
    modify(a[R], 1);
  }
  while (L > l) {
    ans += query(n) - query(a[--L]);
    modify(a[L], 1);
  }
  while (R > r) {
    modify(a[R], -1);
    ans -= query(a[R--]);
  }
  while (L < l) {
    modify(a[L], -1);
    ans -= query(n) - query(a[L++]);
  };
  return ans;
}

void solve(int s, int t, int l, int r, int k) {
  if (s > t) return;
  int mid = (s + t) >> 1, p = l;
  for (int i = l; i <= std::min(mid - 1, r); ++i) {
    if (f[k - 1][i] + calc(i + 1, mid) < f[k][mid]) {
      f[k][mid] = f[k - 1][i] + calc(i + 1, mid);
      p = i;
    }
  }
  solve(s, mid - 1, l, p, k);
  solve(mid + 1, t, p, r, k);
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("CF868F Yet Another Minimization Problem.in", "r", stdin);
  freopen("CF868F Yet Another Minimization Problem.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  memset(f, 0x3f, sizeof(f));
  f[0][0] = 0;
  for (int i = 1; i <= k; ++i) { solve(1, n, 0, n, i); }
  printf("%lld", f[k][n]);
  return 0;
}